#!/bin/bash

echo "=========================================="
echo "服务状态检查脚本"
echo "=========================================="

# 检查Zookeeper服务
echo "1. 检查Zookeeper服务状态..."
if nc -z localhost 2181 2>/dev/null; then
    echo "   ✓ Zookeeper服务正在运行 (端口2181)"
    # 获取Zookeeper进程信息
    ZK_PID=$(lsof -ti:2181)
    if [ ! -z "$ZK_PID" ]; then
        echo "   进程ID: $ZK_PID"
        echo "   进程信息: $(ps -p $ZK_PID -o pid,ppid,command --no-headers)"
    fi
else
    echo "   ✗ Zookeeper服务未运行 (端口2181)"
fi

echo ""

# 检查Kafka服务
echo "2. 检查Kafka服务状态..."
if nc -z localhost 9092 2>/dev/null; then
    echo "   ✓ Kafka服务正在运行 (端口9092)"
    # 获取Kafka进程信息
    KAFKA_PID=$(lsof -ti:9092)
    if [ ! -z "$KAFKA_PID" ]; then
        echo "   进程ID: $KAFKA_PID"
        echo "   进程信息: $(ps -p $KAFKA_PID -o pid,ppid,command --no-headers)"
    fi
    
    # 检查Kafka topic
    echo "   检查Kafka topics..."
    kafka-topics --list --bootstrap-server localhost:9092 2>/dev/null | head -5
    if [ $? -eq 0 ]; then
        echo "   ✓ Kafka连接正常"
    else
        echo "   ✗ Kafka连接异常"
    fi
else
    echo "   ✗ Kafka服务未运行 (端口9092)"
fi

echo ""

# 检查Flink服务
echo "3. 检查Flink服务状态..."
if nc -z localhost 8081 2>/dev/null; then
    echo "   ✓ Flink Web UI正在运行 (端口8081)"
    FLINK_PID=$(lsof -ti:8081)
    if [ ! -z "$FLINK_PID" ]; then
        echo "   进程ID: $FLINK_PID"
        echo "   进程信息: $(ps -p $FLINK_PID -o pid,ppid,command --no-headers)"
    fi
else
    echo "   ✗ Flink Web UI未运行 (端口8081)"
fi

# 检查Flink JobManager
if nc -z localhost 6123 2>/dev/null; then
    echo "   ✓ Flink JobManager正在运行 (端口6123)"
else
    echo "   ✗ Flink JobManager未运行 (端口6123)"
fi

# 检查Flink TaskManager
if nc -z localhost 6124 2>/dev/null; then
    echo "   ✓ Flink TaskManager正在运行 (端口6124)"
else
    echo "   ✗ Flink TaskManager未运行 (端口6124)"
fi

echo ""

# 检查Java进程中的Flink相关进程
echo "4. 检查Flink相关Java进程..."
FLINK_JOBS=$(ps aux | grep -i flink | grep -v grep)
if [ ! -z "$FLINK_JOBS" ]; then
    echo "   找到Flink相关进程:"
    echo "$FLINK_JOBS" | while read line; do
        echo "   $line"
    done
else
    echo "   ✗ 未找到Flink相关进程"
fi

echo ""

# 检查项目特定的Flink作业
echo "5. 检查项目Flink作业..."
PROJECT_JOBS=$(ps aux | grep "HotItemsWindowTopN\|eagle-etl" | grep -v grep)
if [ ! -z "$PROJECT_JOBS" ]; then
    echo "   找到项目相关进程:"
    echo "$PROJECT_JOBS" | while read line; do
        echo "   $line"
    done
else
    echo "   ✗ 未找到项目Flink作业进程"
fi

echo ""

# 检查端口占用情况
echo "6. 相关端口占用情况..."
echo "   端口2181 (Zookeeper): $(lsof -ti:2181 2>/dev/null || echo '未占用')"
echo "   端口9092 (Kafka): $(lsof -ti:9092 2>/dev/null || echo '未占用')"
echo "   端口8081 (Flink Web UI): $(lsof -ti:8081 2>/dev/null || echo '未占用')"
echo "   端口6123 (Flink JobManager): $(lsof -ti:6123 2>/dev/null || echo '未占用')"
echo "   端口6124 (Flink TaskManager): $(lsof -ti:6124 2>/dev/null || echo '未占用')"
echo "   端口22222 (Flink REST API): $(lsof -ti:22222 2>/dev/null || echo '未占用')"

echo ""
echo "=========================================="
echo "检查完成"
echo "=========================================="
